package com.bangdream.michelia.sql.greendao;

import android.database.sqlite.SQLiteDatabase;

import com.bangdream.michelia.application.MainApplication;
import com.bangdream.michelia.utils.PublicResources;

/**
 * Created by Mr.Wang on 2017/9/13.
 * 909732184@qq.com
 */

public class GreenDaoManager {
    private DaoMaster mDaoMaster;
    private static DaoSession mDaoSession;
    private static volatile GreenDaoManager mInstance = null;
    private GreenDaoManager(){
        if (mInstance == null) {

            //创建数据库shop.db"
            DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(MainApplication.getContext(), PublicResources.DATABASE_NAME, null);
            //获取可写数据库
            SQLiteDatabase db = helper.getWritableDatabase();
            //获取数据库对象
            mDaoMaster = new DaoMaster(db);
            //获取Dao对象管理者
            mDaoSession = mDaoMaster.newSession();
        }
    }

    public static GreenDaoManager getInstance() {
        if (mInstance == null) {
            synchronized (GreenDaoManager.class) {
                if (mInstance == null) {
                    mInstance = new GreenDaoManager();
                }
            }
        }
        return mInstance;
    }
    public DaoMaster getMaster() {
        return mDaoMaster;
    }
    public DaoSession getSession() {
        return mDaoSession;
    }
    public DaoSession getNewSession() {
        mDaoSession = mDaoMaster.newSession();
        return mDaoSession;
    }
    public static DaoSession getDaoInstant() {
        return mDaoSession;
    }
}
